<?php
/*------------------------------------------------------------------------------ 
    Create Date :   25/10/2010
    Author    :      Nguyen Hoang Phuc
    Copyright(c) 2010 A.D.A Solution. All rights reserved
     
      
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#             Date          Author       Note
    Lephan  TRB1           2010/01/20      HoangPhuc    Xử lý xem bản đồ trường hợp Phương tiện có check tổng hợp
------------------------------------------------------------------------------*/
if (!defined('IN_ADAGPS')){
    die(SYSTEM_ACCESS_DENIED);
} 

global $objTemplate, $objDbSelect,$arrConfig;   

// dang ky su dung ham xajax 
$objXajax->registerFunction('ajxSaveData');
$objXajax->registerFunction('ajxUpdateData');
$objXajax->registerFunction('ajxDrag');
$objXajax->registerFunction('ajxLoadData');
$xxxx = '<img src="images/high_tatras_min.jpg" class="gallery" alt="The peaks of High Tatras" width="96" height="72" /> ';
// load dữ liệu
function ajxLoadData($load = 0)
{
   global $objDbSelect,$objTemplate;
   $now = date('Y-m-d H:i:s'); 
   // load danh sach nhân viên trực
   $sql = "SELECT id,username,url_image,is_service FROM tbl_employee WHERE is_service = 0";   
   $xjax_respone = new xajaxResponse();
   $rs = $objDbSelect->Execute($sql);
   $sScript = ''; 
   if($rs->RecordCount() > 0)
   {
       do
       {
            $id = $rs->fields['id']; 
            $url_image = $rs->fields['url_image'];
            $username = $rs->fields['username'];    
            $sScript .= ' <img src="'.$url_image.'" id="'.$id.'" style="padding: 0.4em; margin: 0 0.4em 0.4em 0;display: inline-block" class="gallery ui-draggable" alt="The peaks of High Tatras" width="96" height="72" /> ';
       }                                           
       while($rs->MoveNext());    
   }
    $xjax_respone = new xajaxResponse();     
    $xjax_respone->addAssign('gallery', 'innerHTML', $sScript);
    $js = "";  
   // load nhan vien dang phục vụ bàn nào
   if ($load == 0 )
   {
       $sql = "SELECT e.id as id_image,b.id,e.username,e.url_image FROM tbl_job b LEFT JOIN tbl_employee e ON b.id_employee = e.id WHERE 1 = 1";   
       $rs = $objDbSelect->Execute($sql);
       $sScript = '<table cellpadding="5" cellspacing="5" border ="0" width="100%" align="center"><tr>';  
       if($rs->RecordCount() > 0)
       {
           $i = 1;
           do
           {
                $id_image = $rs->fields['id_image'];
                $id_job = $rs->fields['id'];   
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : '';  
                $sScript .= '<td  align="center"> <b> Bàn '.$id_job.'</b> <div id="trash'.$i.'" class="ui-widget-content" style="text-align: left;width:150px;height:100px;border: 1px solid #aaaaaa">';
                $sScript .= '<input type="hidden" id = "'.$id_job.'" value="0" />';
                if ($url_image != '')
                {
                  $sScript .= '<span>';
                  $sScript .= ' <img src="'.$url_image.'" id="'.$id_image.'" class="gallery ui-draggable" alt="The peaks of High Tatras" width="96" height="72" style=" width: 48px; height: 36px;padding: 0.4em; margin: 0 0.4em 0.4em 0;display: inline-block"/> ';
                  $sScript .= '</span>';    
                }
                $sScript .= '</div></td>';
                $js .=' var $trash'.$i.' = $( "#trash'.$i.'" );    
                      $( "img", $trash'.$i.' ).draggable({
                        revert: "invalid", 
                        containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
                        helper: "clone",
                        cursor: "move"
                        });
                      $trash'.$i.'.droppable({
                activeClass: "ui-state-highlight",
                drop: function( event, ui ) {
                    var a = $.trim($($(this).find("img").get(0)).attr("id")); 
                    var job_new = $.trim($($(this).find("input").get(0)).attr("id"));   
                    var b = $.trim($($(ui.draggable).get(0)).attr("id"));
                    deleteImage( ui.draggable,$trash'.$i.' ); 
                    xajax_ajxUpdateData(a,b,job_new);
                }
           });';
                $i++;
           }                                           
           while($rs->MoveNext());    
       }
       $sScript .= '</tr></table>';
       $xjax_respone->addAssign('demo', 'innerHTML', $sScript); 
   }
    $xjax_respone->addScript('$(function() {
            var $gallery = $( "#gallery" );
            $( "img", $gallery ).draggable({
            revert: "invalid", 
            containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
            helper: "clone",
            cursor: "move"
            });
            $gallery.droppable({
            activeClass: "custom-state-active",
            drop: function( event, ui ) {
                var a = $.trim($($(ui.draggable).get(0)).attr("id"));
                var trash = $.trim($($(ui.draggable).parent().parent().get(0)).attr("id"));
                var job = $.trim($($("#"+trash).find("input").get(0)).attr("id"));
                recycleImage( ui.draggable ,$gallery);
                xajax_ajxUpdateData(a,"",job); 
            }
        });
        '.$js.'
            var recycle_icon = "";
        function deleteImage( $item ,$trash) {
            $item.fadeOut(function() {
                var $list = $( "span", $trash ).length ?
                    $( "span", $trash ) :
                    $( "<span/>" ).appendTo( $trash );
                 $trash.find( "img" ).remove();    
                $item.find( "a.ui-icon-trash" ).remove();
                $item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
                    $item
                        .animate({ width: "48px",height: "36px" })
                        .find( "img" )
                            .animate({ height: "36px",width:"36px"});
                });
            });
        }
        var trash_icon = "";
        function recycleImage( $item ,$gallery) {
            $item.fadeOut(function() {
                $item
                    .find( "a.ui-icon-refresh" )
                        .remove()
                    .end()
                    .css({
                         height : "72px",
                         width : "96px",
                         display: "inline-block"         
                    })
                    .append( trash_icon )
                    .find( "img" )
                        .css( {
                         height : "72px",
                         width : "96px",
                         display: "inline-block"   
                        } )
                    .end()
                    .appendTo( $gallery )
                    .fadeIn();
            });
            }
    });'); 
   return $xjax_respone -> getXML();                  
}
// cập nhật nhân viên phục vụ bàn
function ajxUpdateData($a, $b,$job_new)
{
  global $objDbSelect;
   $xjax_respone = new xajaxResponse(); 
  if ($a != '')
  {
  $sql = "UPDATE tbl_employee SET is_service = '0' WHERE id = '$a'";
  $sql3 = "UPDATE tbl_job SET id_employee = 0 WHERE id = '$job_new'"; 
                     
   $objDbSelect->Execute($sql);
   $objDbSelect->Execute($sql3);
   
  }
  $sql2 ="";
  if ($b != '')
  {
   $sql2 = "UPDATE tbl_employee SET is_service = '1' WHERE id = '$b'";   
   $objDbSelect->Execute($sql2);
   $sql3 = "UPDATE tbl_job SET id_employee = 0 WHERE id_employee = $b";
   $objDbSelect->Execute($sql3);      
   $sql4 = "UPDATE tbl_job SET id_employee = $b WHERE id = '$job_new'"; 
   $objDbSelect->Execute($sql4);       
    $xjax_respone->addScript('xajax_ajxLoadData(1);');              
  }
  
  
  return $xjax_respone -> getXML();             
}   

?>
